namespace PHSRAG.Insight21.FormsManagement.Controls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Collections;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using PHSRAG.Insight21.CDS;
	using System.Text.RegularExpressions;

	/// <summary>
	///	BaseQuestionItemEditor displays common questions like Name, Text etc while creating a new question(QuestionnaireItem).
	///	
	///	This class has no other functionality except to display text, allow user to enter/change values 
	///	and give them back to its container, all through properties
	/// </summary>
	public partial class BaseQuestionItemEditor : System.Web.UI.UserControl
	{
		#region Member variables
		private bool suppressValidation;
		private bool isVersionDeployed;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets whether or not the version is deployed.
		/// </summary>
		public bool IsVersionDeployed
		{
			get { return isVersionDeployed; }
			set { isVersionDeployed = value; }
		}
		
		
		/// <summary>
		/// Gets or sets Name of the QuestionnaireItem
		/// </summary>
		public string ItemName
		{
			get { return itemName.Text; }
			set { itemName.Text = value; }
		}


		/// <summary>
		/// Gets or sets the Question Text.
		/// While getting the text, the paragraph tags are removed and breaks are inserted appropriately.
		/// </summary>
		public string ItemText
		{
			get 
			{ 
				string text = itemText.Text.Replace("\n", string.Empty).Replace("\t", string.Empty).Replace("\r", string.Empty); 
				text = Regex.Replace(text, @"<[pP]>.*?</[pP]>", new MatchEvaluator(BaseQuestionItemEditor.ParagraphTagProcessor));
				return text;
			}
			set { itemText.Text = value; }
		}


		/// <summary>
		/// Gets or sets whether or not the item is required
		/// </summary>
		public bool Required
		{
			get { return itemRequired.Checked; }
			set { itemRequired.Checked = value; }
		}


		/// <summary>
		/// Gets or sets the validation message if the question is not answered
		/// </summary>
		public string ValidationMessage
		{
			get { return validationMessage.Text; }
			set { validationMessage.Text = value; }
		}


		/// <summary>
		/// Gets or sets whether or not a seperator is displayed after the question
		/// </summary>
		public bool HasSeparator
		{
			get { return bool.Parse(horizontalLine.SelectedValue); }
			set { horizontalLine.SelectedValue = value.ToString().ToLower(); }
		}

		
		/// <summary>
		/// Sets whether or not the Required and ValidationMessage controls should be displayed.
		/// </summary>
		public bool SuppressValidation
		{
			set { suppressValidation = value; }
		}
		#endregion

		#region Events (page)
		/// <summary>
		/// Displays the validation panel based on the SuppressValidation flag
		/// </summary>
		/// <param name="sender">Object that raises the event.</param>
		/// <param name="e">Arguments of the event.</param>
		protected void OnPageLoad(object sender, System.EventArgs e)
		{
			if (suppressValidation)
			{
				validationPanel.Visible = false;
			}
			else
			{
				validationMessage.Enabled = itemRequired.Checked;
			}

			if (isVersionDeployed)
			{
				itemName.Enabled = false;
			}
			//The user can enter the validation message only if the question is marked as required.
			itemRequired.Attributes.Add("OnClick", 
						"javascript:ToggleValMsg('" + itemRequired.ClientID + "', '" + validationMessage.ClientID + "')");
		}
		#endregion

		#region Static methods
		/// <summary>
		/// This method is invoked for each match of a regularexpression and removes the paragraph tags. Unlike for the first match,
		/// the subsequent matches insert a break in place of a paragraph.
		/// </summary>
		/// <param name="m">Matched string</param>
		/// <returns>Modified string after processing the paragraph tags</returns>
		private static string ParagraphTagProcessor(Match m)
		{
			string x = m.ToString();
			if (m.Groups[0].Index == 0)
			{
				x = Regex.Replace(x, "<[pP]>", string.Empty);
				x = Regex.Replace(x, "</[pP]>", string.Empty);
			}
			else
			{
				x = Regex.Replace(x, @"<[pP]>(<(BR|br) />)?", "<BR />");
				x = Regex.Replace(x, @"</[pP]>", string.Empty);
			}
			return x;
		}
		#endregion

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.OnPageLoad);

		}
		#endregion
	}
}
